package cn.haut.online_read.crawler;

import cn.haut.online_read.enrity.BookInfo;
import cn.haut.online_read.utils.toMapList;
import lombok.extern.slf4j.Slf4j;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.*;

/**
 * 追书神器 分类 (Category) 页面爬取
 *
 * @author xing
 * @date 2021-04-20 21:02
 */
@Slf4j
public class CategoryCrawler implements PageProcessor {

    private Site site = Site.me().setSleepTime(1);

    @Override
    public void process(Page page) {
        // 分类
        page.putField("gender", page.getHtml().xpath("//div[@class='c-full-sideBar']/a/span/text()").all());
        page.putField("major", page.getHtml().xpath("//div[@class='sort-cells']/a/text()").all());
        List<String> major = page.getHtml().xpath("//div[@class='sub-sort-cells']/p/a/text()").all();
        //   p 标签下，第一个 a 标签 是 "具体类型" 字符串，不是需要的 分类
        major.remove(0);
        page.putField("minor", major);
        page.putField("type", page.getHtml().xpath("//div[@class='more-cells']/a/text()").all());

        // 书籍
        List<String> nameList = page.getHtml().xpath("//div[@class='books-list']/a/div/h4[@class='name']/span/text()").all();
        List<String> authorList = page.getHtml().xpath("//div[@class='books-list']/a/div/p[@class='author']/span[1]/text()").all();
        List<String> descList = page.getHtml().xpath("//div[@class='books-list']/a/div/p[@class='desc']/text()").all();
        // 1 代表获取第一个元素（不是第二个）
        List<String> popularityList = page.getHtml().xpath("//div[@class='books-list']/a/div/p[@class='popularity']/span[@class='c-red'][1]/text()").all();
        // span[@class='c-red'][2] 获取为空，可能是 span[2] class 不是 'c-red'，而不是获取 所有 class='c-red' 的第二个
        List<String> keepList = page.getHtml().xpath("//div[@class='books-list']/a/div/p[@class='popularity']/span[3]/text()").all();
        List<String> idList = page.getHtml().xpath("//div[@class='books-list']/a/@href").replace("/book/","").all();
        List<String> imgUrlList = page.getHtml().xpath("//div[@class='books-list']/a/img/@src").all();

//        List<Map<String, String>> list = new ArrayList<>();
        // name:           书名
        // author:         作者名
        // id:             书的 id
        // desc:           书的简介
        // popularity:     人气值
        // keep:           读者留存率
        List<BookInfo> bookInfoList = BookInfo.getBookInfoList(idList, nameList, authorList, descList, imgUrlList, keepList, popularityList);
        page.putField("bookInfos", bookInfoList);

        String totalPage = page.getHtml().xpath("//span[@class='total']/span/text()").get();
        page.putField("total", Integer.parseInt(totalPage));
    }

    @Override
    public Site getSite() {
        return site;
    }

}
